package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.ResponseBean;
import com.example.demo.enums.ResponseCode;
import com.example.demo.impl.DaHuaShipService;
import com.example.demo.utils.GZipUtils;
import com.example.demo.utils.ListUtils;
import com.example.demo.utils.LonLatUtil;
import com.example.demo.vo.AisShipDaHuaVo;
import com.example.demo.vo.AisShipVo;
import com.google.gson.Gson;
import io.micrometer.core.instrument.util.StringUtils;
import net.ryian.commons.DateUtils;
import org.apache.commons.httpclient.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: xumf
 * @Date: 2019/12/19 14:54
 */
@RestController
public class ShipController {

    @Autowired
    private DaHuaShipService daHuaShipService;

    @RequestMapping("getShipTotalNow")
    public String getZsAreaShipCount(){
        String result=  daHuaShipService.getAisShipTotalToday();
        return result;
    }

    /**
     * 查询12层级以下的ais船舶数据-可视化
     *
     * @param minLon
     * @param maxLon
     * @param minLat
     * @param maxLat
     * @param precision
     * @param
     * @param
     * @return
     */
    @GetMapping(value = "/ais/map/{minLon}/{maxLon}/{minLat}/{maxLat}/{precision}")
    public String queryAisMap(@PathVariable("minLon") double minLon, @PathVariable("maxLon") double maxLon,
                              @PathVariable("minLat") double minLat, @PathVariable("maxLat") double maxLat,
                              @PathVariable("precision") int precision) {
        String result = daHuaShipService.getAisShipMapList(minLon, maxLon, minLat, maxLat, precision);
        return result;
    }

    /**
     * 获取ais船舶数据--可视化
     *
     * @param minLon
     * @param maxLon
     * @param minLat
     * @param maxLat
     * @return
     * @throws Exception
     */
    @GetMapping(value = "shipList")
    public ResponseBean shipLists(Double minLon, Double minLat, Double maxLon, Double maxLat, HttpServletResponse response) throws IOException {
        String result = daHuaShipService.getAisShipList(minLon, maxLon, minLat, maxLat);
        JSONObject json = JSONObject.parseObject(result);
        JSONArray jsonArray = json.getJSONArray("data");
        List<AisShipDaHuaVo> list= jsonArray.toJavaList(AisShipDaHuaVo.class);
        return ResponseBean.success(list);
    }

    /**
     *
     * @param minLon
     * @param minLat
     * @param maxLon
     * @param maxLat
     * @return
     * @throws IOException
     */
    @RequestMapping("shipListYw")
    public ResponseBean shipListYw(Double minLon, Double minLat, Double maxLon, Double maxLat) throws IOException {
        String result = daHuaShipService.getAisShipList(minLon, maxLon, minLat, maxLat);
        JSONObject json = JSONObject.parseObject(result);
        JSONArray jsonArray = json.getJSONArray("data");
        List<AisShipDaHuaVo> list= jsonArray.toJavaList(AisShipDaHuaVo.class);
        JSONObject object = new JSONObject();
        object.put("type", "FeatureCollection");
        JSONArray features = new JSONArray();

        for (AisShipDaHuaVo aisShipVo:list) {
            Double lon = aisShipVo.getLon();
            Double lat = aisShipVo.getLat();
            if (lon == null || lat == null) {
                continue;
            }

            JSONObject o1 = new JSONObject();
            o1.put("type", "Feature");

            JSONObject o2 = new JSONObject();
            o2.put("iconImage",StringUtils.isBlank(aisShipVo.getColor())?"#3adeab":aisShipVo.getColor());
            o2.put("name", StringUtils.isBlank(aisShipVo.getName()) ? "--" : aisShipVo.getName());
            o2.put("mmsi", aisShipVo.getMmsi() == null ? "--" : aisShipVo.getMmsi());
            o2.put("lastTime", aisShipVo.getTime() == null ? "---" : DateUtils.formatTime(new Date(aisShipVo.getTime())));
            o2.put("courseOverGround", aisShipVo.getCourseOverGround());
            o2.put("lon", LonLatUtil.DDtoDMS(lon, true));
            o2.put("lat", LonLatUtil.DDtoDMS(lat, false));
            o2.put("delayState", aisShipVo.getDelayState());
            o2.put("navigationStatus", aisShipVo.getNavigationStatus());
            o2.put("headDirector", aisShipVo.getHeadDirector());
            o1.put("properties", o2);

            JSONObject o3 = new JSONObject();
            o3.put("type", "Point");
            JSONArray a1 = new JSONArray();
            a1.add(lon);
            a1.add(lat);
            o3.put("coordinates", a1);
            o1.put("geometry", o3);
            features.add(o1);
        }
        object.put("features", features);
        System.out.println(features);
        String encode = URLEncoder.encode(object.toString(), "utf-8");
        //处理空格变成＋号的问题
        String replace = encode.replace("+", "%20");
        byte[] compress = GZipUtils.compress(replace.getBytes());
        return new ResponseBean(ResponseCode.SUCCESS, compress);
    }

    /**
     * 获取船只详情--业务系统
     *
     * @param mmsi
     */
    @GetMapping(value = "shipInfo")
    public ResponseBean getShipInfoForYw(@RequestParam("mmsi") Long mmsi, @RequestParam("iState") Integer iState) {
        String result = daHuaShipService.getShipInfo(mmsi, iState);
        JSONObject json = JSONObject.parseObject(result);
        if (json == null) {
            return new ResponseBean(200, "没有查询到数据", "");
        } else {
            String data = json.getString("data");
            JSONObject object = JSON.parseObject(data);
            return ResponseBean.success(object.toString());
        }
    }

}
